package com.cpfmat.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.cpfmat.entity.SystemUser;

public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest httpRequest, HttpServletResponse httpResponse, Object arg2,
			Exception arg3) throws Exception {
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
		String urlString = request.getRequestURI();
		LogUtils.debug("拦截请求：");
		LogUtils.debug(urlString);
		
		if("/manage".equals(urlString)){
			return true;
		}
		
		SystemUser user = (SystemUser) request.getSession().getAttribute("loginUser");
		if(user != null && StringUtils.isNotBlank(user.getSeqId()) ){
			return true;
		}
		
		
		// 请求的路径
		String contextPath = request.getContextPath();

		response.sendRedirect(contextPath + "/manage");
		return false;
	}

}